package scope

import "gorm.io/gorm"

func WithPreload(relation string, relations ...string) IScope {
	return PreloadRule{
		Relation:  relation,
		Relations: relations,
	}
}

type PreloadRule struct {
	Relation  string
	Relations []string
}

func (p PreloadRule) MakeScope() Scope {
	return func(db *gorm.DB) *gorm.DB {
		if len(p.Relations) > 0 {
			for _, r := range p.Relations {
				db = db.Preload(r)
			}
		}

		if p.Relation != "" {
			db = db.Preload(p.Relation)
		}
		return db
	}
}
